package com.music.service.music.impl;

import com.music.dto.music.DownloadDTO;
import com.music.dto.utils.PagingDTO;
import com.music.dto.utils.TableReqDTO;
import com.music.dto.utils.TableRspDTO;
import com.music.entity.music.DownloadEntity;
import com.music.mapper.music.DownloadMapper;
import com.music.service.music.DownloadService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DownloadServiceImpl implements DownloadService {
    @Resource
    private DownloadMapper downloadMapper;

    private final Logger logger = LoggerFactory.getLogger(PlaylistServiceImpl.class);

    @Override
    public TableRspDTO list4Table(Long userId, TableReqDTO tableReqDTO) throws Exception {
        Long count = downloadMapper.count4Table(
                userId,
                tableReqDTO.parseQueryParam("songName"),
                Integer.parseInt(tableReqDTO.parseQueryParam("downloadStatus"))
        );
        List<DownloadDTO> listDownload = downloadMapper.list4Table(
                tableReqDTO.getStart(),
                tableReqDTO.getPageSize(),
                userId,
                tableReqDTO.parseQueryParam("songName"),
                Integer.parseInt(tableReqDTO.parseQueryParam("downloadStatus")),
                tableReqDTO.getSortField(),
                tableReqDTO.getSortOrder());
//        for (int i = 0; i < listDownload.size(); i++)
//            listDownload.get(i).setOrderNumber(1 + i + tableReqDTO.getStart());
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                listDownload);
        return new TableRspDTO(pagingDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(Long userId, DownloadDTO downloadDTO) throws Exception {
        downloadDTO.setUserId(userId);
        downloadDTO.setGmtCreate(new Date());
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        DownloadEntity downloadEntity = mapperFactory.getMapperFacade().map(downloadDTO, DownloadEntity.class);
        downloadMapper.add(downloadEntity);
        return downloadEntity.getDownloadId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(Long downloadId,Integer status) throws Exception {
        return downloadMapper.update(downloadId,status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String downloadIds) throws Exception {
        String[] ids = downloadIds.split(",");
        for (String id : ids) {
            downloadMapper.remove(Long.parseLong(id));
        }
        return ids.length;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer judgeFailed() throws Exception {
        return downloadMapper.judgeFailed();
    }
}
